package com.jzy.hzit.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.Optional;


@RestController
@RequestMapping("/swagger-resources")
public class SwaggerHandlerController {

	private final SwaggerResourcesProvider swaggerResourcesProvider;
	private final SecurityConfiguration securityConfig;
	private final UiConfiguration uiConfig;

	// Constructor with default values for optional dependencies
	public SwaggerHandlerController(SwaggerResourcesProvider swaggerResourcesProvider,
									Optional<SecurityConfiguration> securityConfigOptional,
									Optional<UiConfiguration> uiConfigOptional) {
		this.swaggerResourcesProvider = swaggerResourcesProvider;
		this.securityConfig = securityConfigOptional.orElse(SecurityConfigurationBuilder.defaultSecurityConfiguration());
		this.uiConfig = uiConfigOptional.orElse(UiConfigurationBuilder.defaultUiConfiguration());
	}

	// Autowired constructor for Spring to inject dependencies
	@Autowired
	public SwaggerHandlerController(SwaggerResourcesProvider swaggerResourcesProvider,
									SecurityConfiguration securityConfiguration,
									UiConfiguration uiConfiguration) {
		this(swaggerResourcesProvider,
				Optional.ofNullable(securityConfiguration),
				Optional.ofNullable(uiConfiguration));
	}

	@GetMapping("/configuration/security")
	public Mono<ResponseEntity<SecurityConfiguration>> getSecurityConfiguration() {
		return Mono.just(ResponseEntity.ok(this.securityConfig));
	}

	@GetMapping("/configuration/ui")
	public Mono<ResponseEntity<UiConfiguration>> getUiConfiguration() {
		return Mono.just(ResponseEntity.ok(this.uiConfig));
	}

	@GetMapping("")
	public Mono<ResponseEntity<Object>> getSwaggerResources() {
		return Mono.just(ResponseEntity.ok(this.swaggerResourcesProvider.get()));
	}

	// Static methods to provide default configurations (simulate builders)
	private static class SecurityConfigurationBuilder {
		public static SecurityConfiguration defaultSecurityConfiguration() {
			// Return a default SecurityConfiguration instance
			return new SecurityConfiguration(); // Assuming a no-arg constructor exists
		}
	}

	private static class UiConfigurationBuilder {
		public static UiConfiguration defaultUiConfiguration() {
			// Return a default UiConfiguration instance
			return new UiConfiguration(); // Assuming a no-arg constructor exists
		}
	}
}
